package com.xyf.sys.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wechat.pay.java.service.profitsharing.ProfitsharingService;
import com.wechat.pay.java.service.profitsharing.model.AddReceiverRequest;
import com.wechat.pay.java.service.profitsharing.model.AddReceiverResponse;
import com.wechat.pay.java.service.profitsharing.model.ReceiverRelationType;
import com.wechat.pay.java.service.profitsharing.model.ReceiverType;
import com.xyf.sys.config.MiniprogramConfig;
import com.xyf.sys.config.WxConfig;
import com.xyf.sys.domain.OrderProfitsharingReceiver;
import com.xyf.sys.mapper.OrderProfitsharingReceiverMapper;
import com.xyf.sys.service.IOrderProfitsharingReceiverService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author R-H
 * @Date 2024/7/151025
 * @Description
 */
@Service
@Slf4j
public class OrderProfitsharingReceiverServiceImpl implements IOrderProfitsharingReceiverService {

    @Autowired
    private OrderProfitsharingReceiverMapper mapper;
    @Autowired
    private ProfitsharingService profitsharingService;
    @Autowired
    private WxConfig wxConfig;
    @Autowired
    @Qualifier(value = "clientConfig")
    private MiniprogramConfig.MiniConfig clientConfig;

    @Override
    public List<OrderProfitsharingReceiver> getProfitsharingReceiverList(String orderId) {
        return mapper.selectByOrderId(orderId);
    }

    @Override
    public AddReceiverResponse addProfitsharingReceiver(String type, String account, String name, String relationType, String customRelation, BigDecimal ratio, String orderId) {
        AddReceiverRequest receiverRequest = new AddReceiverRequest();
        receiverRequest.setSubMchid(wxConfig.getLaundryMerchant().getMchid());
        receiverRequest.setAppid(clientConfig.getAppid());
        receiverRequest.setType(ReceiverType.valueOf(type));
        receiverRequest.setAccount(account);
        receiverRequest.setName(name);
        receiverRequest.setRelationType(ReceiverRelationType.valueOf(relationType));
        receiverRequest.setCustomRelation(customRelation);
        log.info("add profit receiver body {}", JSONObject.toJSONString(receiverRequest));
        AddReceiverResponse response = profitsharingService.addReceiver(receiverRequest);
        log.info("add profit receiver result {}", JSONObject.toJSONString(response));
        if (response != null) {
            OrderProfitsharingReceiver receiver = new OrderProfitsharingReceiver();
            receiver.setType(type);
            receiver.setAccount(account);
            receiver.setName(name);
            receiver.setRelationType(relationType);
            receiver.setCustomRelation(customRelation);
            receiver.setRatio(ratio);
            receiver.setOrderId(orderId);
            receiver.setStatus("0");
            receiver.setDelFlag("0");
            this.save(receiver);
        }
        return response;
    }

    @Override
    public void save(OrderProfitsharingReceiver receiver) {
        receiver.setCreateTime(new Date());
        mapper.insert(receiver);
    }
}
